package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.common.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.service.ValidateCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName OrderController
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-06-30
 * @Version V1.0
 **/
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Reference
    ValidateCodeService codeService;
    @Reference
    OrderService orderService;

    @RequestMapping("submit")
    public Result submit(@RequestBody Map map) {
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        //如果验证码不通过则返回错误消息
        if (!codeService.checkValidateCode(telephone, RedisMessageConstant.SENDTYPE_ORDER, validateCode)) {
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        //通过dubbo调用服务实现预约逻辑
        map.put("orderType", Order.ORDERTYPE_WEIXIN);
        Result result = orderService.order(map);
        if (result.isFlag()) {
            String orderDate = (String) map.get("orderDate");
            codeService.sendCommonShortMessage(telephone, "success");
        }
        log.debug(MessageConst.ORDER_SUCCESS);
        return result;
    }

    @RequestMapping("findById")
    public Result findById(Integer id) {
        Map map = orderService.findById(id);
        log.debug(map.toString());
        return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,map);
    }
}
